package cn.sxgan.core.controller;


import cn.sxgan.common.anno.RequestAroundLog;
import cn.sxgan.common.anno.WorkTime;
import cn.sxgan.common.entity.sys.GlobalUser;
import cn.sxgan.common.entity.sys.RequestHolder;
import cn.sxgan.common.entity.vo.SysUserVO;
import cn.sxgan.common.enums.ResStatusEnum;
import cn.sxgan.common.response.Result;
import cn.sxgan.core.controller.api.IAuthControllerApi;
import cn.sxgan.core.services.impl.AuthServiceImpl;
import cn.sxgan.core.services.impl.MailSendServiceImpl;
import cn.sxgan.core.services.impl.UserServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description: 认证控制器
 * @Author: sxgan
 * @Date: 2024/11/7 21:36
 * @Version: 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/auth/")
public class AuthController implements IAuthControllerApi {
    
    @Resource
    MailSendServiceImpl mailSendService;
    
    @Resource
    AuthServiceImpl authService;
    
    @Resource
    UserServiceImpl userService;
    
    @WorkTime("获取邮箱验证码")
    @PostMapping("/mailVerifyCode")
    public Result<String> sendVerifyCode(@RequestBody GlobalUser globalUser) {
        String email = globalUser.getEmail();
        if (StringUtils.isBlank(email)) {
            return Result.fail(ResStatusEnum.EXCEPTION_STATUS_900.getMsg());
        }
        return mailSendService.sendVerifyCodeToSpecifiedEmail(email);
    }
    
    @WorkTime("注册")
    @PostMapping("/signup")
    public Result<Map<String, String>> signup(@RequestBody GlobalUser globalUser) {
        return authService.signupUserByEmail(globalUser);
    }
    
    @WorkTime("获取图片验证码")
    @GetMapping("/verifyCodeImg")
    public Result<Map<String, String>> getVerifyCodeImg(HttpServletResponse response) {
        return mailSendService.getVerifyCodeImg(response);
    }
    
    @WorkTime("登录")
    @RequestAroundLog
    @PostMapping("/signin")
    public Result<Map<String, String>> signin(@RequestBody GlobalUser globalUser) {
        return authService.userAuthByEmail(globalUser);
    }
    
    @WorkTime("忘记密码")
    @PostMapping("/updatePwd")
    public Result<String> updatePwd(@RequestBody GlobalUser globalUser) {
        return authService.updatePwd(globalUser);
    }
    
    @WorkTime("获取用户信息")
    @GetMapping("/getSysUserInfo")
    public Result<SysUserVO> getSysUserInfo() {
        GlobalUser currentUser = RequestHolder.getCurrentUser();
        SysUserVO sysUserVO = userService.selectSysUserInfo(currentUser);
        if (sysUserVO == null) {
            return Result.fail(null, ResStatusEnum.EXCEPTION_STATUS_701.getCode(),
                    ResStatusEnum.EXCEPTION_STATUS_701.getMsg());
        }
        return Result.success(sysUserVO);
    }
}